import random
import time
from flask import Blueprint, request, jsonify, json
from flask_restful import Resource, Api
from second_flask.methods import db1
from common.utils.myredis import myredis
from common.utils.get_data import get_data, _get_data
from common.utils.qiniu import generate_qiniu_token

message_bp = Blueprint('message_bp', __name__)

api = Api(message_bp)


@message_bp.route('/publish_message', methods=['POST'])
def publish_message():
    print('数据》》》》', get_data())
    user_id = get_data()['user_id']
    cate_id = get_data()['cate_id']
    print('??????', cate_id)
    title = get_data()['title']
    content = get_data()['content']
    print('>>>>>>', user_id, cate_id, title)
    print('++++++', content)
    number = int(user_id) % 3 + 1
    code = str(user_id) + str(random.randint(10000, 99999)) + str(int(time.time()))
    print('>>>>>>', type(code))
    sql = "insert into message%d(msg_id,cate_id,title,content,user_id) values('%s',%d,'%s','%s',%d)" % (
        int(number), code, int(cate_id), title, content, int(user_id))
    db1.update(sql)
    myredis.l_push('userauditlist', code)
    return jsonify({'code': 200})


@message_bp.route('/get_token', methods=['GET'])
def get_qiniu_token():
    token = generate_qiniu_token()
    return jsonify({'code': 200, 'token': token})





